package com.njlime.setting.service;

import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.setting.entities.Announce;
import com.njlime.setting.entities.AnnounceCategory;
import com.njlime.setting.repository.AnnounceCategoryDao;
import com.njlime.setting.repository.AnnounceDao;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 帮助公告
 * Created by Dean on 2018-01-16.
 */
@Service
public class AnnounceService {
    @Resource
    private AnnounceDao announceDao;
    @Resource
    private AnnounceCategoryDao announceCategoryDao;

    public AnnounceCategory findCategoryOne(int id){
        return announceCategoryDao.findOne(id);
    }

    public PageBean categoryList(PageBean pageBean, AnnounceCategory announceCategory){
        return announceCategoryDao.categoryList(pageBean, announceCategory);
    }

    @Transactional
    public void addCategory(AnnounceCategory announceCategory){
        announceCategory.setCreate_time(new Date());
        announceCategory.setIs_del(GlobalValue.NO);
        announceCategoryDao.save(announceCategory);
    }

    @Transactional
    public void editCategory(AnnounceCategory announceCategory){
        AnnounceCategory ori = findCategoryOne(announceCategory.getId());
        ori.setName(announceCategory.getName());
        announceCategoryDao.save(ori);
    }

    @Transactional
    public void delCategory(int id){
        AnnounceCategory ori = findCategoryOne(id);
        ori.setIs_del(GlobalValue.YES);
        announceCategoryDao.save(ori);
    }

    public Announce findOne(int id){
        return announceDao.findOne(id);
    }

    public PageBean list(PageBean pageBean, Announce announce){
        return announceDao.list(pageBean, announce);
    }

    @Transactional
    public void add(Announce announce){
        announce.setCreate_time(new Date());
        announce.setIs_del(GlobalValue.NO);
        if (announce.getPublish_status() != null && announce.getPublish_status() != GlobalValue.PublishStatus.UNPUBLISHED) {
            announce.setPublish_time(new Date());
        }
        announceDao.save(announce);
    }

    @Transactional
    public void edit(Announce announce){
        announceDao.edit(announce);
    }

    @Transactional
    public void del(Announce ori){
        ori.setIs_del(GlobalValue.YES);
        ori.setUpdate_time(new Date());
        announceDao.save(ori);
    }

    @Transactional(readOnly = false)
    public void togglePublish(Announce announce) {
        if (announce.getPublish_status() != GlobalValue.PublishStatus.PUBLISHED) {
            announce.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
        } else {
            announce.setPublish_status(GlobalValue.PublishStatus.CANCELED);
        }
        announceDao.save(announce);
    }

}
